package com.fastloan.mgt.customer.web;

import com.fastloan.mgt.customer.enums.CustomerEnums;
import com.fastloan.mgt.customer.model.ZyCustomer;
import com.fastloan.mgt.customer.model.ZyCustomerRisk;
import com.fastloan.mgt.customer.model.vo.RiskCustomerExt;
import com.fastloan.mgt.customer.service.ZyCustomerRiskService;
import com.fastloan.mgt.customer.service.ZyCustomerService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.user.model.CmuUser;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.fastloan.utils.ObjUtil;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 风险Controller
 *
 * @author chenkai
 *         createTime 2017-06-2017/6/1 14:53
 */
@RequestMapping("/api/zy/customerrisk")
@RestController
public class RiskController extends BaseController<ZyCustomerRisk> {

    @Autowired
    private ZyCustomerRiskService riskService;

    @Autowired
    private ZyCustomerService customerService;

    /**
     * 获取机构下所有风险客户
     */
    @PostMapping("/risk")
    public ResultTo riskCustomer(String customerName){
        String topOrg = ManagerKit.getTopOrgId();
        Map<String,Object> map = new HashMap<>();
        map.put("topOrgId",topOrg);
        map.put("riskType",CustomerEnums.RiskEnum.RISK.getCode());
        if(!ObjUtil.isEmpty(customerName)){
            map.put("customerName",customerName);
        }
        return dataPage(riskService,RiskCustomerExt.class,"getRisk",map);
    }


    /**
     * 获取机构下所有风险客户
     */
    @PostMapping("/black")
    public ResultTo blackCustomer(String customerName){
        String topOrg = ManagerKit.getTopOrgId();
        Map<String,Object> map = new HashMap<>();
        map.put("topOrgId",topOrg);
        map.put("riskType",CustomerEnums.RiskEnum.BLACK.getCode());
        if(!ObjUtil.isEmpty(customerName)){
            map.put("customerName",customerName);
        }
        return dataPage(riskService,RiskCustomerExt.class,"getRisk",map);
    }


    /**
     * 转入风险
     */
    @PostMapping("/saveRisk")
    public ResultTo entryRisk(ZyCustomerRisk customerRisk){
        String optManagerId = ManagerKit.getUserId();
        String customerManagerId = customerService.selectByPrimaryKey(customerRisk.getCustomerId()).getCustomerManagerId();
        if(optManagerId != null && Objects.equals(optManagerId,customerManagerId)){
            riskService.insertSelective(customerRisk.setCreateTime(new Date())
                    .setCustomerManagerId(customerManagerId));
            return new ResultTo();
        }
        return new ResultTo(ResultEnum.OPERATION_FAILED,"不是对应的客户经理，无法操作");
    }

    /**
     * 删除风险客户
     */
    @PostMapping("/deleteRisk")
    public ResultTo deleteRisk(String customerId){
        String optManagerId = ManagerKit.getUserId();
        String customerManagerId = customerService.selectByPrimaryKey(customerId).getCustomerManagerId();
        if(optManagerId != null && Objects.equals(optManagerId,customerManagerId)){
            riskService.delete(new ZyCustomerRisk().setCustomerId(customerId));
            return new ResultTo();
        }
        return new ResultTo(ResultEnum.OPERATION_FAILED,"不是对应的客户经理，无法操作");
    }

    /**
     * 风险转入黑名单
     */
    @PostMapping("/riskToBlack")
    public ResultTo toBlack(String customerId,String blackReason){
        String optManagerId = ManagerKit.getUserId();
        String customerManagerId = customerService.selectByPrimaryKey(customerId).getCustomerManagerId();
        if(optManagerId != null && Objects.equals(optManagerId,customerManagerId)){
            ZyCustomerRisk customerRisk = riskService.selectOne(new ZyCustomerRisk().setCustomerId(customerId));
            if(customerRisk != null && customerRisk.getRiskType().equals(CustomerEnums.RiskEnum.RISK.getCode())){
                customerRisk.setBlackReason(blackReason)
                        .setRiskType(CustomerEnums.RiskEnum.BLACK.getCode())
                        .setModifyTime(new Date());
                riskService.updateByPrimaryKeySelective(customerRisk);
                return new ResultTo();
            }
            return new ResultTo(ResultEnum.OPERATION_FAILED,"不是风险客户或者风险类型不是->风险");
        }
        return new ResultTo(ResultEnum.OPERATION_FAILED,"不是对应的客户经理，无法操作");
    }

    /**
     * 黑名单转入风险
     */
    @PostMapping("/blackToRisk")
    public ResultTo blackToRisk(String customerId){
        String optManagerId = ManagerKit.getUserId();
        String customerManagerId = customerService.selectByPrimaryKey(customerId).getCustomerManagerId();
        if(optManagerId != null && Objects.equals(optManagerId,customerManagerId)){
            ZyCustomerRisk customerRisk = riskService.selectOne(new ZyCustomerRisk().setCustomerId(customerId));
            if(customerRisk != null && customerRisk.getRiskType().equals(CustomerEnums.RiskEnum.BLACK.getCode())){
                customerRisk.setRiskType(CustomerEnums.RiskEnum.RISK.getCode())
                        .setModifyTime(new Date());
                riskService.updateByPrimaryKeySelective(customerRisk);
                return new ResultTo();
            }
            return new ResultTo(ResultEnum.OPERATION_FAILED,"不是风险客户或者风险类型不是->黑名单");
        }
        return new ResultTo(ResultEnum.OPERATION_FAILED,"不是对应的客户经理，无法操作");
    }











}
